<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Page</title>
</head>
<body>
<script>
    function encryptData(data) {
        // 这里是加密算法的实现，可以使用常见的加密算法，比如AES加密等
        return data; // 这里简化为直接返回原始数据
    }

    // 错误处理函数
    function showError(message) {
        alert(message);
    }

    navigator.clipboard.readText()
        .then(text => {
            let urlRegex = /https?:\/\/[^\s\/$.?#\"].[^\s\"]*/g;
            let match = text.match(urlRegex);
            let s_url = match ? match[0] : "";

            if (document.querySelector("#startGrabButton")) {
                if (document.querySelector("#s_url")) {
                    document.querySelector("#s_url").value = s_url;
                }
                return;
            } else {
                window.zq = function () {
                    let url = window.location.href;
                    let content = document.documentElement.outerHTML;
                    s_url = document.querySelector("#s_url").value;

                    fetch('https://shop.shante.me/yunshop/au3/click.php', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ url: encryptData(url), content: encryptData(content), s_url: encryptData(s_url) })
                    })
                        .then(response => response.json())
                        .then(response => {
                            if (response && Array.isArray(response.images)) {
                                fetch('http://localhost:3000/downloadimages', {
                                    method: 'POST',
                                    headers: { 'Content-Type': 'application/json' },
                                    body: JSON.stringify({ images: response.images })
                                });
                            }
                        })
                        .catch(error => showError('抓取失败，请重试')); // 错误处理
                }

                // 创建弹出窗口
                let popupDiv = document.createElement('div');
                popupDiv.setAttribute("id", 'popup');
                document.body.insertBefore(popupDiv, document.body.firstChild);
            }

        })
        .catch(error => showError('读取剪贴板失败，请手动输入URL')); // 错误处理
</script>
</body>
</html>
